package com.example.music_liwei.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.music_liwei.R;
import com.example.music_liwei.bean.ResponseData;
import com.example.music_liwei.messageEvent.UpdateDrawerEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class FirstBaseQuickAdapter extends BaseQuickAdapter<ResponseData.MusicInfo, BaseViewHolder> {

    public FirstBaseQuickAdapter(int layoutResId, @Nullable List<ResponseData.MusicInfo> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, ResponseData.MusicInfo musicInfo) {
        ImageView imageView_cover = baseViewHolder.getView(R.id.imageView_cover);
        baseViewHolder.setText(R.id.textView_music_name, musicInfo.getMusicName());
        baseViewHolder.setText(R.id.textView_author, musicInfo.getAuthor());
        ImageView imageView2 = baseViewHolder.getView(R.id.imageView2);

        // 使用RequestOptions设置圆角属性
        RequestOptions options = new RequestOptions()
                .transform(new CircleCrop());  // 使用CircleCrop进行圆形转换
        Glide.with(getContext())
                .load(musicInfo.getCoverUrl())
                .apply(options)
                .into(imageView_cover);

        imageView2.setOnClickListener(v -> {
            // 创建放大缩小动画
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView2, "scaleX", 1f, 1.2f, 1f);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView2, "scaleY", 1f, 1.2f, 1f);
            // 创建旋转动画
            ObjectAnimator rotation = ObjectAnimator.ofFloat(imageView2, "rotation", 0f, 360f);
            // 设置动画持续时间
            scaleX.setDuration(1000);
            scaleY.setDuration(1000);
            rotation.setDuration(1000);

            // 创建动画集合
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(scaleX, scaleY, rotation);
            animatorSet.start();

            // 发送事件，通知主页面更新抽屉列表
            EventBus.getDefault().post(new UpdateDrawerEvent(musicInfo));
        });
    }
}
